package com.cirkana.viewer.route;

import java.io.DataInputStream;
import java.io.DataOutputStream;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import com.cirkana.DataResolverUtil;
import com.cirkana.PassData;
import com.cirkana.ViewerActivity;
import com.cirkana.data.DataException;
import com.cirkana.data.DataNotFoundException;
import com.cirkana.data.route.IRouteDetail;
import com.cirkana.serial.bin.BinarySerializationException;
import com.cirkana.serial.bin.BinarySerializationUtil;
import com.cirkana.viewer.R;
import com.cirkana.viewer.data.route.RouteDetail;
import com.cirkana.viewer.data.route.RouteDetailList;
import com.cirkana.viewer.route.RouteDetailActivity.RouteDetailPassData;

public class SelectRouteActivity extends ViewerActivity {

	public static final int REQUEST_ROUTEDETAIL = 1;

	private Spinner routesSourceSpinner;
	private ListView routesListView;
	private RouteDetailList localRoutesList;
	private RouteDetailList remoteRoutesList;
	private int selectedSource = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectroute);

		routesSourceSpinner = (Spinner) findViewById(R.id.selectroute_source);
		routesListView = (ListView) findViewById(R.id.selectroute_routeslist);

		int s = getPreferences().getSelectRouteSource();
		routesSourceSpinner.setSelection(s);
		showRoutes(s);
		routesSourceSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> routesSources, View arg1, int position, long arg3) {
				showRoutes(position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
	}
	
	private void showRoutes(int source) {
		if (source != selectedSource) {
			selectedSource = source;
			routesListView.setAdapter(null);
			routesListView.setOnItemClickListener(null);
			try {
				RouteDetailList routesList = selectedSource == 0 ? getLocalRoutesList() : getRemoteRoutesList();
				routesListView.setAdapter(new ArrayAdapter<IRouteDetail>(this, android.R.layout.simple_list_item_1, routesList));
				routesListView.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

						RouteDetail routeDetail = (RouteDetail) routesListView.getItemAtPosition(position);
						try {
							startActivityForResult(RouteDetailActivity.class, REQUEST_ROUTEDETAIL, new RouteDetailPassData(routeDetail));
						} catch (BinarySerializationException e) {
							showErrorMessage(e);
						}
					}
				});
			} catch (DataNotFoundException e) {
				showErrorMessage(getLiteral(R.string.select_route), getLiteral(R.string.error_no_routes));
			} catch (Exception e) {
				showErrorMessage(e);
			} finally {
				getPreferences().setSelectRouteSource(selectedSource);
			}
		}
	}

	private RouteDetailList getLocalRoutesList() throws DataException {
		if (this.localRoutesList == null)
			this.localRoutesList = (RouteDetailList) DataResolverUtil.getRouteDataResolver().getRouteDetailList();
		return this.localRoutesList;
	}

	private RouteDetailList getRemoteRoutesList() throws DataException {
		if (this.remoteRoutesList == null)
			this.remoteRoutesList = (RouteDetailList) DataResolverUtil.getRouteDataResolver().getRemoteRouteDetailList();
		return this.remoteRoutesList;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case REQUEST_ROUTEDETAIL:
			switch (resultCode) {
			case RESULT_CANCELED:
				break;
			case RESULT_OK:
				try {
					RouteDetailPassData d = (RouteDetailPassData) getResponse(data, RouteDetailPassData.class);
					SelectRoutePassData r = new SelectRoutePassData();
					r.setSelectedRoute(d.getRouteDetail());
					setResponse(RESULT_OK, r);
				} catch (BinarySerializationException e) {
					openMessageBox("Error", e.getMessage());
				}
				finish();
				break;
			}
			break;
		}
	}

	public static class SelectRoutePassData extends PassData {

		private RouteDetail selectedRoute;

		public RouteDetail getSelectedRouteDetail() {
			return this.selectedRoute;
		}

		public void setSelectedRoute(RouteDetail selectedRoute) {
			this.selectedRoute = selectedRoute;
		}

		public SelectRoutePassData() {
			super();
		}

		public SelectRoutePassData(RouteDetail selectedRoute) {
			this.selectedRoute = selectedRoute;
		}

		@Override
		public void serialize(DataOutputStream out) throws BinarySerializationException {
			BinarySerializationUtil.serializeSerializable(this.selectedRoute, out);
		}

		@Override
		public void deserialize(DataInputStream in) throws BinarySerializationException {
			this.selectedRoute = (RouteDetail) BinarySerializationUtil.deserializeSerializable(RouteDetail.class, in);
		}

		@Override
		public String toString() {
			StringBuilder s = new StringBuilder("{");

			s.append("selectedRoute=");
			if (this.selectedRoute == null)
				s.append("(null)");
			else
				s.append(this.selectedRoute.toString());
			s.append("}");

			return s.toString();
		}

	}
}
